package cn.cmcc.intertelecom.utils;

import static org.hamcrest.CoreMatchers.instanceOf;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.cmcc.intertelecom.bean.Msg;
import cn.cmcc.intertelecom.bean.User;

/**
 * 用于处理web的一些工具
 * @author wp
 *
 */
public class WEBUtils {

	/**
	 * 获取需要访问的权限名称
	 * @param joinPoint
	 * @return
	 */
	public static String getAccessPower(JoinPoint joinPoint) {
		String power = null;
		//获取类名
	    String targetName = joinPoint.getTarget().getClass().getName();
	    //获取方法名称
	    String Methodname = joinPoint.getSignature().getName();
	    try {
	    	//通过反射获取该类的方法
			Class<?> targetClass = Class.forName(targetName);
			Method[] methods = targetClass.getDeclaredMethods();
			for (Method method : methods) {
				if(method.getName().equals(Methodname)){
					//获取该方法的注解
					 ArchivesLog log = method.getAnnotation(ArchivesLog.class);
					 //获取注解中的方法
					 power = log.operationPower();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return power;
	}
	
	/**
	 * 获取已登录的用户
	 * @return
	 */
	public static User getLoginUser(){
		HttpSession session = SysContext.getSession();
		Object object = session.getAttribute("loginUser");
        //判断object是否可以转换为user
        if(object instanceof User){
        	User loginUser = (User) session.getAttribute("loginUser");
    	    return loginUser;
        }
        return null;
	    
	}

}
